package com.tadpole.music.view.activity;

import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.RadioButton;
import android.widget.Toast;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import com.tadpole.music.R;
import com.tadpole.music.adapter.base.HomePagerAdapter;
import com.tadpole.music.app.MyApplication;
import com.tadpole.music.bean.PushBean;
import com.tadpole.music.view.activity.base.BaseActivity;
import com.tadpole.music.view.event.JumpOneEvent;
import com.tadpole.music.view.event.StartPlayEvent;
import com.tadpole.music.view.event.StopPlayEvent;
import com.tadpole.music.view.fragment.home.HomeFragment;
import com.tadpole.music.view.fragment.me.MeFragment;
import com.tadpole.music.view.fragment.question.QuestionFragment;
import com.tadpole.music.view.fragment.study.StudyFragment;
import com.tadpole.music.view.widget.NoScrollViewPager;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import java.util.ArrayList;


public class MainActivity extends BaseActivity {
    private NoScrollViewPager viewPager;
    private ArrayList<Fragment> fragments;
    private RadioButton mRadio1;
    private RadioButton mRadio2;
    private RadioButton mRadio3;
    private RadioButton mRadio4;
    private long exitTime = 0L;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        //注册
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        initViews();
        initFragment();
    }

    private void initViews() {
        viewPager = findViewById(R.id.viewPager);
        mRadio1 = (RadioButton) findViewById(R.id.mRadio1);
        mRadio2 = (RadioButton) findViewById(R.id.mRadio2);
        mRadio3 = (RadioButton) findViewById(R.id.mRadio3);
        mRadio4 = (RadioButton) findViewById(R.id.mRadio4);
    }

    private void initFragment() {
        fragments = new ArrayList<>();
        fragments.add(HomeFragment.newInstance("", ""));
        fragments.add(QuestionFragment.newInstance("", ""));
        fragments.add(StudyFragment.newInstance("", ""));
        fragments.add(MeFragment.newInstance("", ""));
        FragmentManager fm = getSupportFragmentManager();
        HomePagerAdapter adapter = new HomePagerAdapter(fm, fragments);
        viewPager.setAdapter(adapter);

        viewPager.setOffscreenPageLimit(4);

        PushBean pushBean = (PushBean) getIntent().getSerializableExtra("params");
        if(pushBean!=null){
            if(0==pushBean.getMainIndex()){
                viewPager.setCurrentItem(0);
                mRadio1.setChecked(true);
                mRadio2.setChecked(false);
                mRadio3.setChecked(false);
                mRadio4.setChecked(false);
            }else if(1==pushBean.getMainIndex()){
                viewPager.setCurrentItem(1);
                mRadio1.setChecked(false);
                mRadio2.setChecked(true);
                mRadio3.setChecked(false);
                mRadio4.setChecked(false);
            }else if(2==pushBean.getMainIndex()){
                viewPager.setCurrentItem(2);
                mRadio1.setChecked(false);
                mRadio2.setChecked(false);
                mRadio3.setChecked(true);
                mRadio4.setChecked(false);
            }else if(3==pushBean.getMainIndex()){
                viewPager.setCurrentItem(3);
                mRadio1.setChecked(false);
                mRadio2.setChecked(false);
                mRadio3.setChecked(false);
                mRadio4.setChecked(true);
            }
        }


    }

    public void doClick(View view) {
        switch (view.getId()) {
            case R.id.mRadio1:
                viewPager.setCurrentItem(0);
                EventBus.getDefault().post(new StartPlayEvent());
                break;
            case R.id.mRadio2:
                viewPager.setCurrentItem(1);
                EventBus.getDefault().post(new StopPlayEvent());
                break;
            case R.id.mRadio3:
                viewPager.setCurrentItem(2);
                EventBus.getDefault().post(new StopPlayEvent());
                break;
            case R.id.mRadio4:
                viewPager.setCurrentItem(3);
                EventBus.getDefault().post(new StopPlayEvent());
                break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (KeyEvent.KEYCODE_BACK == keyCode && KeyEvent.ACTION_DOWN == event.getAction()) {
            // 判断是否在两秒之内连续点击返回键，是则退出，否则不退出
            if (System.currentTimeMillis() - exitTime > 2000) {
                Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
                // 将系统当前的时间赋值给exitTime
                exitTime = System.currentTimeMillis();
            } else {
                try {
                    MyApplication.getInstance().finishManager.removeActivity(MainActivity.class);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                this.finish();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onComment(JumpOneEvent event) {
        viewPager.setCurrentItem(1);
        mRadio1.setChecked(false);
        mRadio2.setChecked(true);
        mRadio3.setChecked(false);
        mRadio4.setChecked(false);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);//取消注册
    }
}